# database/models.py
from sqlalchemy import Column, Integer, String, Date, DateTime, Float, Text, Boolean
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.sql import func

Base = declarative_base()

class StockBasic(Base):
    """股票基本信息表"""
    __tablename__ = 'stock_basic'

    id = Column(Integer, primary_key=True, autoincrement=True)
    symbol = Column(String(20), nullable=False, index=True, comment='股票代码')
    name = Column(String(100), nullable=False, comment='股票名称')
    market = Column(String(10), nullable=False, comment='市场代码(SH/SZ/BJ)')
    industry = Column(String(50), comment='行业分类')
    listing_date = Column(Date, comment='上市日期')
    total_shares = Column(Float, comment='总股本(万股)')
    float_shares = Column(Float, comment='流通股本(万股)')
    status = Column(String(10), default='1', comment='状态(1:上市, 0:退市)')

    # 新增列 - 从 baostock_client 中识别出的字段
    type = Column(String(20), comment='股票类型')  # 从 get_stock_basic 中识别
    country = Column(String(10), default='CN', comment='国家代码')
    currency = Column(String(10), default='CNY', comment='货币代码')
    delisting_date = Column(Date, comment='退市日期')  # 从 get_stock_basic 中识别

    created_time = Column(DateTime, default=func.now())
    updated_time = Column(DateTime, default=func.now(), onupdate=func.now())

    __table_args__ = (
        {'comment': '股票基本信息表'},
    )

class DailyQuote(Base):
    """日线行情数据表"""
    __tablename__ = 'daily_quotes'

    id = Column(Integer, primary_key=True, autoincrement=True)
    symbol = Column(String(20), nullable=False, index=True, comment='股票代码')
    trade_date = Column(Date, nullable=False, index=True, comment='交易日期')
    open_price = Column(Float, comment='开盘价')
    high_price = Column(Float, comment='最高价')
    low_price = Column(Float, comment='最低价')
    close_price = Column(Float, comment='收盘价')
    volume = Column(Float, comment='成交量(手)')
    turnover = Column(Float, comment='成交额(千元)')
    amplitude = Column(Float, comment='振幅(%)')
    change_rate = Column(Float, comment='涨跌幅(%)')
    turnover_rate = Column(Float, comment='换手率(%)')
    pe_ratio = Column(Float, comment='市盈率')
    pb_ratio = Column(Float, comment='市净率')
    ps_ratio = Column(Float, comment='市销率')
    pcf_ratio = Column(Float, comment='市现率')
    is_st = Column(Boolean, default=False, comment='是否ST')

    # 新增列 - 从 baostock_client 中识别出的字段
    data_source = Column(String(20), default='baostock', comment='数据来源')

    created_time = Column(DateTime, default=func.now())

    __table_args__ = (
        {'comment': '日线行情数据表'},
    )

class FinancialData(Base):
    """财务数据表"""
    __tablename__ = 'financial_data'

    id = Column(Integer, primary_key=True, autoincrement=True)
    symbol = Column(String(20), nullable=False, index=True, comment='股票代码')
    report_date = Column(Date, nullable=False, comment='报告期')
    report_type = Column(String(10), nullable=False, comment='报告类型(Q1/HY/Q3/FY)')
    eps = Column(Float, comment='每股收益')
    revenue = Column(Float, comment='营业收入(万元)')
    net_profit = Column(Float, comment='净利润(万元)')
    total_assets = Column(Float, comment='总资产(万元)')
    total_liabilities = Column(Float, comment='总负债(万元)')
    equity = Column(Float, comment='净资产(万元)')
    roe = Column(Float, comment='净资产收益率(%)')
    roa = Column(Float, comment='总资产收益率(%)')
    gross_margin = Column(Float, comment='毛利率(%)')
    net_margin = Column(Float, comment='净利率(%)')

    # 新增列 - 从 baostock_client 中识别出的财务字段
    total_profit = Column(Float, comment='利润总额(万元)')
    operating_profit = Column(Float, comment='营业利润(万元)')
    revenue_growth_rate = Column(Float, comment='营业收入增长率(%)')
    profit_growth_rate = Column(Float, comment='利润总额增长率(%)')
    asset_growth_rate = Column(Float, comment='总资产增长率(%)')
    equity_growth_rate = Column(Float, comment='净资产增长率(%)')
    eps_growth_rate = Column(Float, comment='每股收益增长率(%)')
    asset_turnover = Column(Float, comment='总资产周转率')
    inventory_turnover = Column(Float, comment='存货周转率')
    receivable_turnover = Column(Float, comment='应收账款周转率')
    current_assets = Column(Float, comment='流动资产(万元)')
    current_liabilities = Column(Float, comment='流动负债(万元)')
    asset_liability_ratio = Column(Float, comment='资产负债率(%)')
    data_source = Column(String(20), default='baostock', comment='数据来源')

    created_time = Column(DateTime, default=func.now())

    __table_args__ = (
        {'comment': '财务数据表'},
    )

class DividendData(Base):
    """分红数据表"""
    __tablename__ = 'dividend_data'

    id = Column(Integer, primary_key=True, autoincrement=True)
    symbol = Column(String(20), nullable=False, index=True, comment='股票代码')
    dividend_date = Column(Date, comment='分红日期')
    ex_dividend_date = Column(Date, comment='除权除息日')
    dividend_per_share = Column(Float, comment='每股股利')
    dividend_ratio = Column(Float, comment='股息率(%)')
    update_date = Column(Date, comment='更新日期')

    # 新增列
    data_source = Column(String(20), default='baostock', comment='数据来源')

    created_time = Column(DateTime, default=func.now())

    __table_args__ = (
        {'comment': '分红数据表'},
    )

class UpdateLog(Base):
    """数据更新日志表"""
    __tablename__ = 'update_log'

    id = Column(Integer, primary_key=True, autoincrement=True)
    data_type = Column(String(20), nullable=False, comment='数据类型(basic/daily/financial/dividend)')
    symbol = Column(String(20), comment='股票代码')
    start_date = Column(Date, comment='开始日期')
    end_date = Column(Date, comment='结束日期')
    records_updated = Column(Integer, default=0, comment='更新记录数')
    status = Column(String(10), nullable=False, comment='状态(success/failed)')
    error_message = Column(Text, comment='错误信息')
    update_time = Column(DateTime, default=func.now())

    __table_args__ = (
        {'comment': '数据更新日志表'},
    )